package com.cuitrip.business.bill;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cuitrip.apiservice.OrderBusiness;
import com.cuitrip.apiservice.model.ApiManager;
import com.cuitrip.app.UserConfig;
import com.cuitrip.business.bill.model.BankInfoList;
import com.cuitrip.business.bill.model.UserAccount;
import com.cuitrip.business.bill.model.UserAccounts;
import com.cuitrip.business.tripservice.event.ServiceChangeEvent;
import com.cuitrip.component.choicedialog.ChoiceDialog;
import com.cuitrip.service.R;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragment;
import com.lab.network.model.CtApiResponse;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.rx.bus.RxBus;
import com.lab.utils.ToastUtil;
import com.lab.utils.Utils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by sheena on 16/4/27.
 */
public class BindPayPalFragment extends CustomUiFragment implements IProxyCallback{

    @Bind(R.id.editMInput1)
    EditText editMInput1;

    @Bind(R.id.submit)
    TextView submit;

    @Bind(R.id.ll)
    LinearLayout llLayout;


    private ApiProxy apiProxy = new ApiProxy(this);
    private String type = "2";//1、银行卡 2、paypal

    public static BindPayPalFragment newInstance() {
        BindPayPalFragment bindPayPalFragment = new BindPayPalFragment();
        Bundle bundle = new Bundle();
        bindPayPalFragment.setArguments(bundle);
        return bindPayPalFragment;
    }

    @Override
    public boolean initCloseTopBar() {
        return false;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        CustomUiConfig config=new CustomUiConfig();
        config.mTopbarMiddleAreaName=getString(R.string.account_paypal);
        config.mTopbarLeftAreaName=getString(R.string.back_icon);
        return config;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState,R.layout.activity_bind_paypal);
    }

    @Override
    public void initView() {
        ButterKnife.bind(this,getView());
    }

    private boolean isValid() {
        if (TextUtils.isEmpty(editMInput1.getText())) {
            Utils.shakeView(editMInput1);
            return false;
        }
        String trim = editMInput1.getText().toString().trim();
        if(!Utils.isEmail(trim)){
            ToastUtil.toast(getHostActivity(), getString(R.string.wallet_account_error), 0);
            return false;
        }

        return true;
    }

    @Override
    public void onTopbarLeftPress() {
        onBackPressed();
    }

    @OnClick(R.id.submit)
    public void submit(){
        final String result = editMInput1.getText().toString();
        if (!isValid()){
            return;
        }
        ChoiceDialog choiceDialog = ChoiceDialog.getInstance();
        choiceDialog.setDialogTitle(getResources().getString(R.string.account_paypal));
        choiceDialog.setDialogTitleDividerVisibility(false);
        choiceDialog.setDialogMessage(getResources().getString(R.string.account_paypal_set_confirm, result));
        choiceDialog.setDialogCoupleStyleSetting(
                getResources().getString(R.string.operation_yes),
                getResources().getString(R.string.operation_cancel), new ChoiceDialog.DialogStyleCoupleCallback() {
                    @Override
                    public boolean onPositiveButtonClick() {
                        UserConfig.setPaypalAccount(result);
                        OrderBusiness.addUserAccount(apiProxy, type, editMInput1.getText().toString(), "", "");
                        return false;
                    }

                    @Override
                    public boolean onNegativeButtonClick() {
                        return false;
                    }
                });
        showDialogFragment(choiceDialog);
    }

    @Override
    public boolean onProxyResult(ProxyResult<?> result) {
        if (!isAdded()){
            return false;
        }
        CtApiResponse response = (CtApiResponse) result.getData();
        if (response.isResponseNormal()) {
            if (response.getApiName().equals(ApiManager.ADD_USER_ACCOUNT)) {
                RxBus.getDefault().send(new AccountAddEvent());
                onBackPressed();
            }
        } else {
            ToastUtil.toast(getContext(), response.msg, 0);
        }
        return false;
    }
}
